<!DOCTYPE html>
<html>
<head>
  <title>Research software development</title>

  <link rel="stylesheet" href="/rsd/assets/css/main.css">
  <link rel="shortcut icon" type="image/png" href="/rsd/assets/favicon.ico"/>

  <link rel="stylesheet"
        href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
  <script
src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>

   
<style media="screen" type="text/css">


div.input_prompt {
  color: #303F9F;
}

div.output_prompt {
  color: #D84315;
}
</style>

    <!-- Loading mathjax macro -->
    <!-- Load mathjax -->
        <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
        <!-- MathJax configuration -->
        <script type="text/x-mathjax-config">
        MathJax.Hub.Config({
            tex2jax: {
                inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                processEscapes: true,
                processEnvironments: true
            },
            // Center justify equations in code and markdown cells. Elsewhere
            // we use CSS to left justify single line equations in code cells.
            displayAlign: 'center',
            "HTML-CSS": {
                styles: {'.MathJax_Display': {"margin": 0}},
                linebreaks: { automatic: true }
            }
        });
        </script>
        <!-- End of mathjax configuration --></head>


    <!-- Google Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-38016329-2', 'auto');
    ga('send', 'pageview');
    </script>

</head>
<body>

    <header class="site-header">

  <div class="wrap">


    <nav class="site-nav">
      <a href="#" class="menu-icon">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
           viewBox="0 0 18 15" enable-background="new 0 0 18 15" xml:space="preserve">
          <path fill="#505050" d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0
            h15.031C17.335,0,18,0.665,18,1.484L18,1.484z"/>
          <path fill="#505050" d="M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0c0-0.82,0.665-1.484,1.484-1.484
            h15.031C17.335,6.031,18,6.696,18,7.516L18,7.516z"/>
          <path fill="#505050" d="M18,13.516C18,14.335,17.335,15,16.516,15H1.484C0.665,15,0,14.335,0,13.516l0,0
            c0-0.82,0.665-1.484,1.484-1.484h15.031C17.335,12.031,18,12.696,18,13.516L18,13.516z"/>
        </svg>
      </a>
      <div class="trigger">
          <a class="page-link" href="/rsd/">RSD</a>
          <a class="page-link" href="/rsd/chapters/">Chapters</a>
          <a class="page-link" href="/rsd/extras/">Extras</a>
      </div>
    </nav>

  </div>
</header>

 <div class="page-content">
    <div class="wrap">
      <div class="home">
        
    <h1>Introduction to Python</h1>
<p>The programming language we will use in this course is Python.</p>
<p>Python is a popular language in industry and research with a number of libraries
very useful for mathematics:</p>
<ul>
<li>Symbolic mathematics with <code>sympy</code></li>
<li>Numerical mathematics with <code>numpy</code></li>
<li>Machine learning with <code>sklearn</code></li>
<li>Data analysis with <code>pandas</code></li>
</ul>
<p>In this chapter we will go over some basic Python.</p>
<h2>Using the Python interpreter</h2>
<p>Open your command line and type:</p>
<pre><code class="bash">python
</code></pre>

<p>This will start a prompt that looks something like:</p>
<pre><code class="bash">
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; 

</code></pre>

<p>The <code>&gt;&gt;&gt;</code> indicates point at which you can type python code.</p>
<p>Type <code>2 + 2</code> and press enter. You can see what this looks like below:</p>
<pre><code class="python">&gt;&gt;&gt; 2 + 2
4

</code></pre>

<h2>Creating numeric variables</h2>
<p>We can assign variables to values using the <code>=</code> operator:</p>
<pre><code class="python">&gt;&gt;&gt; the_meaning_of_life = 42
&gt;&gt;&gt; the_meaning_of_life = the_meaning_of_life + 2
&gt;&gt;&gt; the_meaning_of_life
44

</code></pre>

<h2>Creating boolean variables</h2>
<p>We can create boolean values using a number of comparison operators which
include:</p>
<ul>
<li><code>==</code> equals</li>
<li><code>!=</code> not equals</li>
<li><code>&gt;</code> strictly greater</li>
<li><code>&gt;=</code> greater than or equal</li>
</ul>
<pre><code class="python">&gt;&gt;&gt; is_42 = the_meaning_of_life == 42
&gt;&gt;&gt; is_42
False
&gt;&gt;&gt; greater_than_42 = the_meaning_of_life &gt; 42
&gt;&gt;&gt; greater_than_42
True

</code></pre>

<h2>Creating list variables</h2>
<p>Python has an indexable structure called lists:</p>
<pre><code class="python">&gt;&gt;&gt; numbers = [1, 2, 4, 5]
&gt;&gt;&gt; max(numbers)
5
&gt;&gt;&gt; min(numbers)
1
&gt;&gt;&gt; sum(numbers)
12
&gt;&gt;&gt; numbers[0]
1
&gt;&gt;&gt; numbers[-2]
4
&gt;&gt;&gt; numbers.append(50)
&gt;&gt;&gt; numbers
[1, 2, 4, 5, 50]

</code></pre>

<p>To close the python interpreter type:</p>
<pre><code>exit()
</code></pre>

<h2>Using Python scripts</h2>
<p>To start to write more sophisticated code (software) we can write code
inside a file which we can then run using the command line.</p>
<h3>Writing a python script</h3>
<p>Open one of the recommended text editors and create a new file in the
<code>rsd-workshop</code> (created in <a href="../01/">Chapter 01</a>) save it as <code>01-hello-world.py</code>.
Inside the file write the following (ignore the <code>&gt;&gt;&gt;</code>):</p>
<pre><code class="python">print(&quot;hello world&quot;)
</code></pre>

<h3>Running a python script</h3>
<p>Using the command line, let us now "run" this file using the command line:</p>
<pre><code class="bash">python 01-hello-world.py
</code></pre>

<p>You can in fact run various programming languages this way: saving them to a
file in your editor of choice and running them with the correct command.</p>
<p>This allows for flexibility as you can choose an editor that has features you
enjoy and customise it to work for you.</p>
<h2>If statements</h2>
<p>We can use boolean variables to create logical statements.</p>
<p>Write a file called <code>02-if-statements.py</code>, include the following code and run
it.</p>
<pre><code class="python">N = 572
if N % 2 == 0:
    print(&quot;N is even&quot;)
else:
    print(&quot;N is odd&quot;)

</code></pre>

<p><strong>Note</strong> white space and indentation is important in python. The indented code
block indicate what code to execute if the boolean variable <code>N % 2 == 0</code> is
<code>True</code>.</p>
<h2>While loops</h2>
<p>It is possible to repeat code using <code>while</code> loops which will repeatedly check a
boolean variable.</p>
<p>Write a file called <code>03-while-loops.py</code>, include the following code and run
it.</p>
<pre><code class="python">N = 0
even_number_count = 0
while N &lt; 10:
    if N % 2 == 0:
        even_number_count = even_number_count + 1
    N = N + 1
print(even_number_count)

</code></pre>

<h2>Functions</h2>
<p>It is possible to create functions in python which we will use later to write
modular code.</p>
<p>Write a file called <code>04-functions.py</code>, include the following code and run it.</p>
<pre><code class="python">def count_even_numbers(upper_limit):
    N = 0
    even_number_count = 0
    while N &lt; upper_limit:
        if N % 2 == 0:
            even_number_count = even_number_count + 1
        N = N + 1
    return even_number_count
print(count_even_numbers(10))
print(count_even_numbers(42))


</code></pre>

<h1>Tip</h1>
<p>Explore your editor: both Atom and VS Code offer many plugins for Python. This
is not just a luxury to make things easier but <strong>also</strong> helps avoid mistakes by
letting the plugins check various things for you.</p>
<p>This is not just the case for these editors and Python. Most good editors have a
full ecosystem of tools available to support the writing of code in many
languages.</p>
<h1>Optional</h1>
<p>Write some code to compute and compare both sides of the following equation, for
varying values of $N$.</p>
<p>$$
\sum_{i=1}^Ni=\frac{N(N+1)}{2}
$$</p>

      </div>
  </div>
</div>


</body>
</html>